package com.example.side_pull_menu;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;

import com.example.side_pull_menu.Fragment.Fragment_2;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

import static android.support.constraint.Constraints.TAG;

public class PieChartUtil3 {
    private ListView lv;
    public final int[] PIE_COLORS={
            Color.rgb(181, 194, 202), Color.rgb(129, 216, 200), Color.rgb(241, 214, 145),
            Color.rgb(108, 176, 223), Color.rgb(195, 221, 155), Color.rgb(251, 215, 191),
            Color.rgb(237, 189, 189), Color.rgb(172, 217, 243)
    };

    public interface BlogService {
        @GET("aircondition")
        Call<ResponseBody> getBlog();
    }
    String[] name={
            "星期一",
            "星期二",
            "星期三",
            "星期四",
            "星期五",
            "星期六",
            "星期天",
    };
    private static PieChartUtil3 pieChartUtil;
    public List<PieEntry>entries;
    public static PieChartUtil3 getPieChart(){
        if (pieChartUtil==null){
            pieChartUtil=new PieChartUtil3();
        }
        return pieChartUtil;
    }
    public void setPieChart(PieChart pieChart, Map<String, Float> pieValues, String title, boolean showLegend){
        pieChart.setUsePercentValues(true);//设置百分比
        pieChart.getDescription().setEnabled(false);//设置描述
        pieChart.setRotationEnabled(true);//是否可以旋转
        pieChart.setHighlightPerTapEnabled(true);//点击是否放大
        pieChart.setDrawCenterText(true);//设置文字环绕中心
        pieChart.setDrawHoleEnabled(true);//这个方法为true就是环形图，false就是饼图
        pieChart.setExtraOffsets(0,0,0,0);//设置边距
        // 0表示摩擦最大，基本上一滑就停
        // 1表示没有摩擦，会自动转化为0.9999,及其顺滑
        pieChart.setDragDecelerationFrictionCoef(0.5f);//设置滑动时的摩擦系数（值越小摩擦系数越大）
        pieChart.setCenterText(title);//设置环中的文字
        pieChart.setCenterTextSize(15f);//设置环中文字的大小
        pieChart.setCenterTextColor(PIE_COLORS[2]);
        pieChart.setRotationAngle(120f);//设置旋转角度
        pieChart.setTransparentCircleRadius(61f);//设置半透明圆环的半径,看着就有一种立体的感觉
        //设置环形中间空白颜色是白色
        pieChart.setHoleColor(Color.TRANSPARENT);
        //设置半透明圆环的颜色
        pieChart.setTransparentCircleColor(Color.WHITE);
        //设置半透明圆环的透明度
        pieChart.setTransparentCircleAlpha(110);
        //图例设置
        Legend legend=pieChart.getLegend();
        if (showLegend){
            legend.setEnabled(true);//是否显示图例
            legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例相对于图表横向的位置
            legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例相对于图表纵向的位置
            legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例显示的方向
            legend.setDrawInside(false);
            legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//方向
        }else{
            legend.setEnabled(false);
        }

        //设置饼图数据
        setPieChartData1(pieChart);
        pieChart.animateX(1500, Easing.EasingOption.EaseInOutQuad);//数据显示动画
    }
    //设置饼图数据
    public void setPieChartData1(final PieChart pieChart) {
//        //遍历HashMap
//        Set set=pieValues.entrySet();
//        Iterator it = set.iterator();//得到适配器
//        entries=new ArrayList<>();
//        while (it.hasNext()) {
//            Map.Entry entry = (Map.Entry) it.next();
//            entries.add(new PieEntry(Float.valueOf(entry.getValue().toString()), entry.getKey().toString()));
//        }
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        RequestBody requestBody = (RequestBody) retrofit.create(BlogService.class);
        BlogService service = retrofit.create(BlogService.class);
        Call<ResponseBody> call = service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                entries = new ArrayList<>();
                try {
                    Gson gson1 = new Gson();
                    Fragmentkj3 carlist=gson1.fromJson(response.body().string(),Fragmentkj3.class);
                    for (int i=0;i<carlist.get拥挤程度().size();i++){
                        entries.add(new PieEntry((float)carlist.get拥挤程度().get(i),name[i]));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                PieDataSet dataSet = new PieDataSet(entries,"");
                dataSet.setSliceSpace(3f);//设置饼块之间的间隔
                dataSet.setSelectionShift(6f);//设置饼块选中时偏离饼图中心的距离
                dataSet.setColors(PIE_COLORS);//设置饼块的颜色
                dataSet.setValueTextSize(5f);
                //设置数据显示方式有见图
                dataSet.setValueLinePart1OffsetPercentage(80f);//数据连接线距图形片内部边界的距离，为百分数
                dataSet.setValueLinePart1Length(0.3f);
                dataSet.setValueLinePart2Length(0.4f);
                dataSet.setValueLineColor( PIE_COLORS[3]);//设置连接线的颜色
                dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//y轴数据显示在饼图内/外
                dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//x轴数据显示在饼图内/外
                PieData pieData = new PieData(dataSet);
                pieData.setValueFormatter(new PercentFormatter());
                pieData.setValueTextSize(11f);
                pieData.setValueTextColor(Color.DKGRAY);
                pieChart.setData(pieData);
                pieChart.highlightValues(null);
                pieChart.invalidate();
            }
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
